package com.herui.iot.ui.main;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.herui.sdyu_lib.base.BaseFragment;
import com.herui.sport_care.R;
import com.herui.sport_care.app.SportCareApp;
import com.herui.sport_care.ui.BaseSportActivity;

public class MainActivity extends BaseSportActivity {

    SportCareApp sportCareApp = SportCareApp.getInstance();

    @BindView(R.id.viewPager)
    ViewPager2 viewPager;
    @BindView(R.id.imgv_management)
    ImageView management;
    @BindView(R.id.imgv_report)
    ImageView report;
    @BindView(R.id.imgv_mine)
    ImageView mine;
    @BindView(R.id.tv_management)
    TextView tvManagement;
    @BindView(R.id.tv_report)
    TextView tvReport;
    @BindView(R.id.tv_mine)
    TextView tvMine;

    private BaseFragment[] baseFragments = {HomeFragment.newInstance(),
            ReportFragment.newInstance(), MimeFragment.newInstance()};

    private String[] permissionArray = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_CONNECT
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sportCareApp.setEnterMain(true);

        ButterKnife.bind(this);
        askMultiplePermission(permissionArray);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initParam() {
        viewPager.setAdapter(fragmentPagerAdapter);
    }

    @OnClick({R.id.view_main, R.id.view_report, R.id.view_mime})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.view_main:
                viewPager.setCurrentItem(0);
                management.setImageResource(R.mipmap.ic_management_active);
                report.setImageResource(R.mipmap.ic_report_normal);
                mine.setImageResource(R.mipmap.ic_mine_normal);

                tvManagement.setTextColor(0xFF63E6BE);
                tvReport.setTextColor(0xffacacac);
                tvMine.setTextColor(0xffacacac);
                break;
            case R.id.view_report:
                viewPager.setCurrentItem(1);
                management.setImageResource(R.mipmap.ic_management_normal);
                report.setImageResource(R.mipmap.ic_report_active);
                mine.setImageResource(R.mipmap.ic_mine_normal);

                tvManagement.setTextColor(0xffacacac);
                tvReport.setTextColor(0xFF63E6BE);
                tvMine.setTextColor(0xffacacac);
                break;
            case R.id.view_mime:
                viewPager.setCurrentItem(2);
                management.setImageResource(R.mipmap.ic_management_normal);
                report.setImageResource(R.mipmap.ic_report_normal);
                mine.setImageResource(R.mipmap.ic_mine_active);

                tvManagement.setTextColor(0xffacacac);
                tvReport.setTextColor(0xffacacac);
                tvMine.setTextColor(0xFF63E6BE);
                break;
            default:
                break;
        }
    }

    private FragmentStateAdapter fragmentPagerAdapter =
            new FragmentStateAdapter(this) {
                @Override
                public int getItemCount() {
                    return 3;
                }

                @NonNull
                @Override
                public Fragment createFragment(int position) {
                    return baseFragments[position];
                }

            };

}